﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Galaktika.BI.Silverlight.ClientApp.Web.Commands
{
    using Galaktika.BI.Runtime.Services;
    using Galaktika.BI.Silverlight.ClientApp.Web.Runtime;
    using Galaktika.BI.Silverlight.ClientApp.Web.DAL;

    public class GetVariablesCommand : ServerCommand
    {
        public GetVariablesCommand(InvokeSchema invoker)
            : base(Galaktika.BI.Silverlight.Commands.CommandId.GetVariablesId)
        {
            this.ContextId = invoker[KnownInvokeArgs.ContextId];

            if (this.ContextId == null)
            {
                throw new ArgumentNullException(KnownInvokeArgs.ContextId);
            }
        }

        public string ContextId { get; private set; }

        public override InvokeResultSchema Invoke()
        {
            InvokeResultSchema res = new InvokeResultSchema(base.Id);
            DataBinder binder = DataBinder.Current;
            VariablesContainer vars = binder.GetVariables(this.ContextId);
            res.ContentType = KnownContentTypes.Variables;
            DalVariablesRoot container = new DalVariablesRoot();
            container.Vars.AddRange(vars.GetAsList());
            res.Content = XmlUtility.Obj2XmlStr(container);

            return res;
        }
    }
}
